#include "Subsector.h"
#include "error.h"

Subsector::Subsector() : Node()
{
	mObject = 0;
}

Subsector::Subsector(GameObject* gp, int x, int y) : Node(x, y)
{
	mObject = gp;
}

Subsector::~Subsector()
{
	DestroyGameObject();
}

// Should be called by another Subsector to steal the object in this one
void Subsector::StealGameObject(Subsector &s)
{
	// could have an error in this code
	// either stealing an object while we already have one
	// or stealing a non-existant object
	try
	{
		// if the object exists, steal it
		if (s.mObject)
		{
			if (mObject)
			{
				// ERROR message here. What will happen to my object?
				error* e = new error;
				e->msg = ERR_STEAL_TARGET_OBJ_ALREADY_EXISTS;
				throw e;
			}

			// grab the other guy's object
			mObject = s.mObject;

			// set his to null
			s.mObject = 0;
		}
		else
		{
			// ERROR message here. Why are we trying to steal
			// from a Subsector without anything?
			error* e = new error;
			e->msg = ERR_STEAL_VICTIM_OBJECT_DOES_NOT_EXIST;
			throw e;
		}
	}

	catch(error* e)
	{
		// catch the error here
		ErrorID error = e->msg;
	}
}

// Called by the main game to destroy the object stored in this Subsector
void Subsector::DestroyGameObject()
{
	// if the object exists, destroy it
	if (mObject)
	{
		delete mObject;
		mObject = 0;
	}
}

// sets the object
void Subsector::SetGameObject(GameObject* obj)
{
	try
	{
		if (mObject)
		{
			// ERROR message here. Object already exists
			// at this Node. What will happen to it?
			error *e = new error;
			e->msg = ERR_SET_OBJECT_ALREADY_EXISTS;
			throw e;
		}
		else
		{
			mObject = obj;
		}
	}

	catch(error* e)
	{
		ErrorID error = e->msg;
	}

}